package com.example.studentregistration.controller;


//文件上传控制器

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.example.studentregistration.util.EditResult;
import com.example.studentregistration.util.Result;
import com.example.studentregistration.vo.EditData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

@RestController
@RequestMapping("/api")
public class FileController {
    //读取文件上传路径
    @Value("${file.upload.path}")
    private String fileUploadPath ;
    //读取文件映射的路径
    @Value("${file.upload.fileMapper}")
    private String fileMapper ;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException
    {
        //1,获取文件名
        String fileName = file.getOriginalFilename() ;
        //2,获取文件后缀
        String fileType = FileUtil.getSuffix(fileName);
        //3,获取文件大小
        long fileSize = file.getSize() ;
        // 创建文件上传目录
        File uploadParentFile = new File(fileUploadPath) ;
        // panentFile.exists()判断uploadParentFile是否存在,不存在则创建
        if (!uploadParentFile.exists()){
            uploadParentFile.mkdirs() ;
        }
        // 定义唯一标识符,重新生成文件名
        String uid = IdUtil.fastSimpleUUID() ;
        //创建文件
        String newFileName = uid + StrUtil.DOT + fileType ;
        //文件地址
        String url = fileMapper + newFileName ;
//        File newFile = new File(fileUploadPath + newFileName) ;
        File newFile = Paths.get(fileUploadPath, newFileName).toFile();
        //将文件保存到指定目录
        file.transferTo(newFile);
        return Result.success(url) ;

    }

    @PostMapping("/editUpload")
    public EditResult editUploadFile(MultipartFile file) throws IOException {
//1获取文件名
        String fileName = file.getOriginalFilename();
//2.获取文件后缀
        String fileType = FileUtil.extName(fileName);
//3.获取文件大小
        long fileSize = file.getSize();
//4.创建文件上传目录
        File uploadParentFile = new File(fileUploadPath);
//panentFile.exists()判断uploadParentFile是否存在,不存在则创建
        if (!uploadParentFile.exists()) {
            uploadParentFile.mkdirs();
        }
////定义唯一标识码，重新命名文件
        String uuid = IdUtil.fastSimpleUUID();
//创建文件
        String newFileName = uuid + StrUtil.DOT + fileType;
//文件地址
        String url = fileMapper + newFileName;
        File newFile = new File(fileUploadPath + newFileName);
//5.将文件保存到指定目录
        file.transferTo(newFile);
        EditResult editResult = new EditResult();
        EditData data = new EditData();
        data.setUrl("http://localhost:8080"+url);
        editResult.setErrno(0); //0表示成功 1表示失败
        editResult.setMessage("上传成功");
        editResult.setData(data);
        return editResult;
    }

}
